package com.gitee.huanminabc.jcommon.multithreading;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @Description:
 * @Author huanmin
 * @Date 2024/6/5 下午1:01
 */
public interface LockReadWriteKey<T> {
    //读锁
    void readLock(T key);
    //尝试读锁
    boolean readTryLock(T key);
    //尝试读锁 阻塞
    boolean readTryLock(T key, long timeout, TimeUnit unit) throws InterruptedException;

    //写锁
    void writeLock(T key);
    //尝试写锁
    boolean tryWriteLock(T key);
    //尝试写锁 阻塞
    boolean tryWriteLock(T key, long timeout, TimeUnit unit) throws InterruptedException;

    //解锁
    void unlock(T key);

    default void writeLock(T key, Runnable runnable) {
        writeLock(key);
        try {
            runnable.run();
        } finally {
            unlock(key);
        }
    }


    default T writeLock(T key, Supplier<T> supplier) {
        writeLock(key);
        try {
            return supplier.get();
        } finally {
            unlock(key);
        }
    }

    default boolean tryWriteLock(T key, Runnable runnable) {
        if (tryWriteLock(key)) {
            try {
                runnable.run();
                return true;
            } finally {
                unlock(key);
            }
        }
        return false;
    }

    default T tryWriteLock(T key, T failResult, Supplier<T> supplier) {
        if (tryWriteLock(key)) {
            try {
                return supplier.get();
            } finally {
                unlock(key);
            }
        }
        return failResult;
    }

    default boolean tryWriteLock(T key, long timeout, TimeUnit unit, Runnable runnable) throws InterruptedException {
        if (tryWriteLock(key, timeout, unit)) {
            try {
                runnable.run();
                return true;
            } finally {
                unlock(key);
            }
        }
        return false;
    }




    default void readLock(T key, Runnable runnable) {
        readLock(key);
        try {
            runnable.run();
        } finally {
            unlock(key);
        }
    }

    default T readLock(T key, Supplier<T> supplier) {
        readLock(key);
        try {
            return supplier.get();
        } finally {
            unlock(key);
        }
    }

    default boolean readTryLock(T key, Runnable runnable) {
        if (readTryLock(key)) {
            try {
                runnable.run();
                return true;
            } finally {
                unlock(key);
            }
        }
        return false;
    }

    default T readTryLock(T key, T failResult, Supplier<T> supplier) {
        if (readTryLock(key)) {
            try {
                return supplier.get();
            } finally {
                unlock(key);
            }
        }
        return failResult;
    }

    default boolean readTryLock(T key, long timeout, TimeUnit unit, Runnable runnable) throws InterruptedException {
        if (readTryLock(key, timeout, unit)) {
            try {
                runnable.run();
                return true;
            } finally {
                unlock(key);
            }
        }
        return false;
    }

}
